package com.ncbee.base.web.base;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.ncbee.base.model.enums.ResultCode;
import com.ncbee.base.model.vo.Result;
import com.ncbee.base.model.vo.UserInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;

/**
 * @Author fengzirong
 * @DESC
 * @Date 2018/12/9 20:22
 **/
@Controller
public class LoginController {
    @Autowired
    private Producer captchaProducer;
    @RequestMapping("/kaptcha")
    public void getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");
        //生成验证码
        String capText = captchaProducer.createText();
        session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
        //向客户端写出
        BufferedImage bi = captchaProducer.createImage(capText);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
    }

    @RequestMapping("loginPage")
    public String loginPage(){
        return "login";
    }

    @ResponseBody
    @RequestMapping(value = "login")
    public Object login(String username,String password,String verifyCode,boolean rememberMe,HttpSession session) throws AuthenticationException{
        if(!StringUtils.isEmpty(verifyCode)){
            String sessionVerifyCode=(String)session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
          int resultFlag=verifyCode.compareToIgnoreCase(sessionVerifyCode);
          if(resultFlag!=0){
              Result result=Result.getInstance(ResultCode.BASE_ERROR);
              result.setMsg("验证码不正确，请重新输入!");
              return result;
          }
        }
        UsernamePasswordToken token=new UsernamePasswordToken(username,password,rememberMe);
        Subject subject=SecurityUtils.getSubject();
        subject.login(token);
        UserInfo userInfo=(UserInfo)subject.getPrincipal();
        //登录成功将用户信息存入Session
        session.setAttribute("loginUser",userInfo);
        Result result=Result.getInstance(ResultCode.SUCCESS);
        result.setMsg("登录成功");
        return result;
    }
}
